package com.zzyl.nursing.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.po.Reservation;
import com.zzyl.nursing.domain.vo.ReservationVO;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.service.IReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.*;
import java.util.Arrays;
import java.util.List;

/**
 * 预约信息Service业务层处理
 *
 * @author alexis
 * @date 2025-06-04
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService {
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     *
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id) {
        return reservationMapper.selectById(id);
    }

    /**
     * 查询预约信息列表
     *
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation) {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation) {
        return reservationMapper.insert(reservation);
    }

    /**
     * 修改预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation) {
        return reservationMapper.updateById(reservation);
    }

    /**
     * 批量删除预约信息
     *
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids) {
        return reservationMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public List<Reservation> selectReservation(Integer status) {
        Long userId = UserThreadLocal.getUserId();
        if (ObjectUtil.isEmpty(userId)) {
            throw new BaseException("请先登录");
        }

        List<Reservation> list = lambdaQuery()
                .eq(Reservation::getCreateBy, userId)
                .eq(status != null, Reservation::getStatus, status)
                .list();

        return list;
    }

    @Override
    public int cancelReservation(Long id) {
        Reservation reservation = getById(id);
        if (ObjectUtil.isEmpty(reservation)) {
            throw new BaseException("预约不存在");
        }
        reservation.setStatus(2);
        reservation.setUpdateBy(UserThreadLocal.getUserId() + "");
        return updateById(reservation) ? 1 : 0;

    }

    @Override
    public List<ReservationVO> getCountByTime(Long time) {
        LocalDateTime now = Instant.ofEpochMilli(time)
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();
        LocalDate nowDate = LocalDate.of(now.getYear(), now.getMonth(), now.getDayOfMonth());
        LocalDateTime start = LocalDateTime.of(nowDate, LocalTime.of(0, 0, 0));
        LocalDateTime end = start.plusDays(1);

        List<ReservationVO> reservationVOList = baseMapper.getCountByTime(now, start, end);


        return reservationVOList;
    }

    @Override
    public Long getCancelledReservationCount(Long userId) {

        LocalDate now = LocalDate.now();
        LocalDateTime start = LocalDateTime.of(now, LocalTime.of(0, 0, 0));
        LocalDateTime end = start.plusDays(1);


        Long count = lambdaQuery().eq(Reservation::getUpdateBy, userId)
                .ge(Reservation::getTime, start)
                .le(Reservation::getTime, end)
                .eq(Reservation::getStatus, 2)
                .count();


        return count;
    }

    /**
     * 删除预约信息信息
     *
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id) {
        return reservationMapper.deleteById(id);
    }
}
